﻿using System.Collections.Immutable;
using System.Diagnostics;

namespace DimensionsHelper.LanguageService.Syntax;

public class SelectCaseClause : BodiedStatement
{
    private readonly SyntaxToken _caseKeywordToken;


    private readonly SyntaxList<SelectCaseComparison> _conditions;
    private readonly SyntaxToken? _elseKeywordToken;

    internal SelectCaseClause(SyntaxToken caseKeywordToken, SyntaxToken? elseKeywordToken, SyntaxNode? parent) :
        base(SyntaxKinds.SelectCaseClause, parent)
    {
        SetChildField(ref _caseKeywordToken, caseKeywordToken);
        SetChildNullableField(ref _elseKeywordToken, elseKeywordToken);
        _conditions = new SyntaxList<SelectCaseComparison>();
    }


    public SyntaxToken CaseKeywordToken => _caseKeywordToken;

    public SyntaxToken? ElseKeywordToken => _elseKeywordToken;

    public bool IsCaseElse => _elseKeywordToken != null;


    public ImmutableArray<SelectCaseComparison> Conditions => [.._conditions];


    public void AddCondition(SelectCaseComparison condition)
    {
        Debug.Assert(_elseKeywordToken == null);
        AddChildMemberToReadOnlyField(_conditions, condition);
    }


    public override void AcceptVisitor<T>(SyntaxNodeVisitor<T> visitor)
    {
        Accept(visitor, CaseKeywordToken, ElseKeywordToken);
        AcceptList(visitor, _conditions);
        base.AcceptVisitor(visitor);
    }
}


public static partial class NodeFactory
{
    public static SelectCaseClause CreateSelectCaseClause(SyntaxNode? parent, SyntaxToken caseKeywordToken,
        SyntaxToken? elseKeywordToken)
    {
        Debug.Assert(caseKeywordToken.IsNode(SyntaxKinds.CaseKeywordToken));
        Debug.Assert(elseKeywordToken == null || elseKeywordToken.IsNode(SyntaxKinds.ElseKeywordToken));
        return new SelectCaseClause(caseKeywordToken, elseKeywordToken, parent);
    }
}